package com.huike.xingxing.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.google.gson.JsonObject;
import com.huike.xingxing.R;
import com.huike.xingxing.api.Api;
import com.huike.xingxing.ui.CircleImageView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class MyDescActivity extends AppCompatActivity {

    private ImageView iv_title_black;
    private TextView tv_title_txt_black;
    private TextView tv_title_txt_content;
    private ImageView iv_title_right_image;
    private TextView tv_title_right_txt;
    private EditText ed_my_desc_pwd;
    private EditText ed_my_desc_pwd_confirm;
    private EditText et_mydesc_user;
    private String username;
    private CircleImageView myimage;
    String image_url;
    RequestQueue requestQueue = null;
    Context context;
    private String uuid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_desc);
        initData();//获取用户缓存信息
        initView();
        context = this;
        requestQueue= Volley.newRequestQueue(this);

    }

    private void initData() {
        SharedPreferences userIfno = getSharedPreferences("user_ifno", MODE_PRIVATE);
        username = userIfno.getString("username", null);
        image_url = userIfno.getString("head_image",null);
         uuid = userIfno.getString("uuid",null);

    }

    private void initView() {
        iv_title_black = (ImageView) findViewById(R.id.iv_title_black);
        tv_title_txt_black = (TextView) findViewById(R.id.tv_title_txt_black);
        tv_title_txt_content = (TextView) findViewById(R.id.tv_title_txt_content);
        iv_title_right_image = (ImageView) findViewById(R.id.iv_title_right_image);
        tv_title_right_txt = (TextView) findViewById(R.id.tv_title_right_txt);

        iv_title_black.setVisibility(View.VISIBLE);
        tv_title_txt_content.setText("信息详细");

        //返回上一界面
        iv_title_black.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ed_my_desc_pwd = (EditText) findViewById(R.id.ed_my_desc_pwd);
        ed_my_desc_pwd_confirm = (EditText) findViewById(R.id.ed_my_desc_pwd_confirm);
        et_mydesc_user = (EditText) findViewById(R.id.et_mydesc_user);
        et_mydesc_user.setText(username);

        myimage = (CircleImageView) findViewById(R.id.myimage);
        Log.e("---", "initView: "+image_url );
        if(image_url!=null){
            Glide.with(this).load(Api.url+image_url).into(myimage);
        }

    }

    //TODO  确定修改密码
    public void my_confirm(View v) {
        String pwd = ed_my_desc_pwd.getText().toString().trim();
        String confirm = ed_my_desc_pwd_confirm.getText().toString().trim();

        if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "密码不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(confirm)) {
            Toast.makeText(this, "请在输入一次密码！", Toast.LENGTH_SHORT).show();
            return;
        }

        if (pwd.equals(confirm)) {
            //提交请求
            JSONObject object = new JSONObject();

            try {
                object.put("id",uuid).put("password",confirm);
                Log.e("---", "my_confirm: "+object.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }

            JsonObjectRequest request = new JsonObjectRequest(1, Api.locaApi + "restPassword",
                    object.toString(), new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {
                        String data = jsonObject.getString("data");

                        if (data.equals("ok")) {
                            Toast.makeText(context, "修改成功！", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(context, "修改失败！", Toast.LENGTH_SHORT).show();

                        }
                    } catch (Exception e) {
                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            });

            requestQueue.add(request);


        } else {
            Toast.makeText(this, "2次密码不匹配！", Toast.LENGTH_SHORT).show();
            return;
        }

    }

    //TODO  qk密码
    public void my_reset(View v) {
        ed_my_desc_pwd.setText("");
        ed_my_desc_pwd_confirm.setText("");
        ed_my_desc_pwd.setHint("");

    }
}
